<?php

/**
 * paypal帐户
 */
class Application_Model_PaypalAccount extends Application_Model_DomainObject {
    
    protected $_id;
    protected $_payer_email;             //
    protected $_payer_id;                //
    protected $_payer_status;            //
    protected $_address_city;            //
    protected $_address_country;         //
    protected $_address_country_code;    //
    protected $_address_name;            //
    protected $_address_state;           //
    protected $_address_status;          //
    protected $_address_street;          //
	protected $_address_zip;             //
	protected $_first_name;              //
	protected $_last_name;               //
	protected $_payer_business_name;     //
	protected $_uuch_user_id;            //
	protected $_uuch_user_ip;            //
	protected $_uuch_user_status;        //	
	protected $_created_at;              //	
	
	
	/**
     * 默认
     */
    const Uuch_Status_Default = 0;	
	
	/**
     * 列为黑名单
     */
	const Uuch_Status_Black = 50;
	
	/**
     * 已认证
     */
    const Uuch_Status_Verified = 100;		
	    
	/**
     * 新增帐号
     */
	 
	public static function addNew($paybill, $params) {
		$userId = $paybill->AccountId;
		$params['uuch_user_id'] = $userId;
		$params['amount'] = $paybill->Money;
		$params['domain_type'] = $paybill->Type;
		$params['domain_id'] = $paybill->DomainId;
		foreach ($params as $key => $value) {
			$params[$key] = trim($value);
		}
		$account = new Application_Model_PaypalAccount();
		$record = $account->queryOne(array('payer_email' => $params['payer_email'], 'uuch_user_id' => $userId));		
		if (!$record || !$record->Id) {	
			$user = Application_Model_User::load($userId);
			$params['uuch_user_ip'] = $user->Loginip;
			$params['uuch_user_status'] = 0;
			$account->setData($params);
			$account->save();
		} else {
			$mapper = $account->getDao()->getMapper();
			foreach ($mapper as $property => $field) {
				$account->$field = $record->$field;
			}
		}
		$account->riskControl($params);
		return $account;
	}
	
	/**
     * 风险控制
     */
	public function riskControl($params) {
		$hasRisk = false;
		$reason = '';
		if ($this->isVerifiedOfPaypal() || $this->isVerifiedOfUuch()) {
			return;
		}
		$data = $this->relationMultiplePaypalAccount();
		if ($data && count($data) > 1) {
			$hasRisk = true;
			$arr = array();
			foreach($data as $item) {
				$payerstatus = $item->payer_status == 'verified' ? '(认证)' : '';
				$arr[] = $item->Payer_email . $payerstatus;
			}
			$reason .= '关联多个paypal帐号:' . implode(',', $arr) . ';<br />';
		}
		$data = $this->relationMultipleUuchAccount();
		if ($data && count($data) > 1) {
			$hasRisk = true;
			$arr = array();
			foreach($data as $item) {
				$arr[] = '<a target="_blank" style="color:blue" href="/admini/manageuser/userdetail/id/' . $item->Uuch_user_id . '">' . $item->Uuch_user_id . '</a>';
			}
			$reason .= '关联多个uuch帐号:' . implode(',', $arr) . ';<br />';
		}
		/*$data = $this->relationMultipleUuchIp();
		if ($data && count($data) > 1) {
			$hasRisk = true;
			$arr = array();
			foreach($data as $item) {
				$arr[] = $item->uuch_user_ip;
			}
			$reason .= '关联多个uuch ip:' . implode(',', $arr) . ';<br />';
		}*/
		if ($this->isEnglishName()) {
			$hasRisk = true;
			$reason .= '英文名;';
		}
		if ($params['amount'] > 10000) {
			$hasRisk = true;
			$reason .= '金额数较大;';
		}
		if ($hasRisk) {
			$params['reason'] = $reason;
			$params['pass'] = 0;
			$params['reason'] = $reason;
			$ids = explode(',', $params['domain_id']);
			foreach ($ids as $domainId) {
			  $risk = new Application_Model_PaypalRisk();
			  $params['domain_id'] = $domainId;
			  $risk->setData($params);
			  $risk->save();
			}
		}
		return $hasRisk;
	}
		
	
	/**
     * paypal已认证
     */
	public function isVerifiedOfPaypal() {
		return strtolower($this->_payer_status) == 'verified';
	}
    
	/**
     * uuch已认证
     */
	public function isVerifiedOfUuch() {
		return $this->_uuch_user_status == 100;
	}		
	
	/**
     * 关联多个paypal帐号
     */
	public function relationMultiplePaypalAccount() {
		$data = $this->getDao()->queryAll(array('uuch_user_id' => $this->_uuch_user_id));
		return $data;
	}
	
	/**
     * 关联多个uuch帐号
     */
	public function relationMultipleUuchAccount() {
		$data = $this->getDao()->queryAll(array('payer_email' => $this->_payer_email));
		return $data;
	}
	
	/**
     * 关联多个uuch ip
     */
	public function relationMultipleUuchIp() {
		$data = $this->getDao()->queryAll(array('uuch_user_ip' => $this->_uuch_user_ip));
		return $data;
	}
	
	/**
     * 是否英文名
     */
	public function isEnglishName() {
		return false;
	}
	
	public static function loadByIdAndEmail($userId, $email) {//payer_email, uuch_user_id
		$object = self::getDao()->queryOne(array('payer_email' => $email, 'uuch_user_id' => $userId));
		return $object;
	}
	
	public static function loadByUserId($userId) {//payer_email, uuch_user_id
		$data = self::getDao()->queryAll(array('uuch_user_id' => $userId));
		return $data;
	}
	
	public static function loadByPaypal($email) {//payer_email, uuch_user_id
		$data = self::getDao()->queryAll(array('payer_email' => $email));
		return $data;
	}
	
	/**
     * 是否黑名单
     */
	public static function isBlack($userId) {
		$object = self::getDao()->queryOne(array('uuch_user_id' => $userId, 'uuch_user_status' => 50));
		return $object && $object->Uuch_user_id > 0;
	}
	/**
     * 是否通过paypal认证
     */
	public static function getFilter($status,$quick, $dateinbegin,$dateinend, $pagesize, $pageindex) {
		//$where = array();
		$and = "";
		$where['1']=1;
		switch ($status) {
			case 'verified':
				$where[' and payer_status =']=$status;//array('payer_status' => $search);
				break;
			case 'unverified':
				$where[' and payer_status =']=$status;//array('payer_status' => $status);
				break;
			case '100':
				$where[' and uuch_user_status =']=$status;//array('uuch_user_status' => $status);
				break;
			case '0':
				$where[' and uuch_user_status =']=$status;//array('uuch_user_status' => $status);
				break;
			case 50:
				$where[' and uuch_user_status =']=$status;//array('uuch_user_status' => $status);
				break;
			
			default:
				break;
		}
		
       
       if($quick != ""){
            $where[" and ( payer_email like "] = "%".$quick."%";	          
            $where[" ) and "] = 1;
            $and = " and ";
        }
        if ($dateinbegin != "") {
            $where[$and . " created_at >= "] = $dateinbegin;
            $and = " and ";
        }
        if ($dateinend != "") {
            $where[$and . " created_at <= "] = $dateinend;
            $and = " and ";
        }
        return self::getDao()->queryPage($where, "created_at desc", $pagesize, $pageindex);
	}
	
}